//
//  LLAddMinusView.m
//  lrlzapp
//
//  Created by dl on 16/2/23.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLAddMinusView.h"

@interface LLAddMinusView ()
@property(nonatomic, weak) IBOutlet UIView *view;
@property(weak, nonatomic) IBOutlet UIButton *minusBtn;
@property(weak, nonatomic) IBOutlet UIButton *addBtn;
@property(weak, nonatomic) IBOutlet UILabel *countLabel;
@end

@implementation LLAddMinusView

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        NSString *className = NSStringFromClass([self class]);
        self.view = [[[NSBundle mainBundle] loadNibNamed:className owner:self options:nil] firstObject];
        [self addSubview:self.view];

        [self reset];
        return self;
    }
    return nil;
}

- (void)reset {
    self.enable = YES;
    self.minValue = 0;
    self.maxValue = 10;
    self.count = 2;

    @weakify(self)

    // 最大、最小值变得时候，适当的修改count
    RAC(self, count) =
            [RACSignal combineLatest:@[RACObserve(self, minValue),
                            RACObserve(self, maxValue)]
                              reduce:^id(id minValue, id maxValue) {
                                  @strongify(self)
                                  if ([minValue integerValue] > self.count) {
                                      return minValue;
                                  } else if ([maxValue integerValue] < self.count) {
                                      return maxValue;
                                  }
                                  return @(self.count);
                              }];

    // 减号按钮 是否可用
    RACSignal *minusEnableSignal =
            [RACSignal combineLatest:@[RACObserve(self, count),
                            RACObserve(self, minValue),
                            [RACSignal return:@(self.enable)]]
                              reduce:^id(id count, id minValue, id enable) {
                                  return @([enable boolValue] && [count integerValue] > [minValue integerValue]);
                              }];
    // 加好按钮 是否可用
    RACSignal *addEnableSignal =
            [RACSignal combineLatest:@[RACObserve(self, count),
                            RACObserve(self, maxValue),
                            [RACSignal return:@(self.enable)]]
                              reduce:^id(id count, id maxValue, id enable) {
                                  return @([enable boolValue] && [count integerValue] < [maxValue integerValue]);
                              }];

    self.minusBtn.rac_command = [[RACCommand alloc] initWithEnabled:minusEnableSignal signalBlock:^RACSignal *(id _) {
        @strongify(self)
        if ([self.delegate respondsToSelector:@selector(addMinusViewWillDecrease)]
                && [self.delegate addMinusViewWillDecrease]) {
            self.count--;
        }
        return [RACSignal empty];
    }];
    self.addBtn.rac_command = [[RACCommand alloc] initWithEnabled:addEnableSignal signalBlock:^RACSignal *(id _) {
        @strongify(self)
        if ([self.delegate respondsToSelector:@selector(addMinusViewWillIncrease)]
                && [self.delegate addMinusViewWillIncrease]) {
            self.count++;
        }
        return [RACSignal empty];
    }];

    [[RACObserve(self, count)
            distinctUntilChanged]
            subscribeNext:^(id count) {
                @strongify(self)
                self.countLabel.text = [NSString stringWithFormat:@"%ld", [count integerValue]];
            }];
}

- (void)setMinValue:(NSInteger)minValue {
    if (_minValue != minValue && minValue <= self.maxValue) {
        _minValue = minValue;
    }
}

- (void)setMaxValue:(NSInteger)maxValue {
    if (_maxValue != maxValue && self.minValue <= maxValue) {
        _maxValue = maxValue;
    }
}

@end
